package com.simon.hse.activity.risk;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.simon.hse.MyApplication;
import com.simon.hse.R;
import com.simon.hse.activity.BaseActivity;
import com.simon.hse.http.CommonCallBack;
import com.simon.hse.http.HttpManager;
import com.simon.hse.http.entity.DropDownInput;
import com.simon.hse.http.entity.DropDownOutput;
import com.simon.hse.utils.ToastUtils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

@SuppressWarnings("all")
@ContentView(R.layout.activity_risk_manager)
public class RiskManagerActivity extends BaseActivity {
    @ViewInject(R.id.tv_common_title)
    TextView mTv_common_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTv_common_title.setText("风险管控信息库");

        initRiskClass();
        initRiskLevel();
    }

    @Event(value = {R.id.iv_common_left, R.id.ll_function_1, R.id.ll_function_2, R.id.ll_function_3, R.id.ll_function_4, R.id.ll_function_5, R.id.ll_function_6, R.id.ll_function_7})
    private void clickFunction(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.iv_common_left:
                finish();
                break;
            case R.id.ll_function_1:
                intent.setClass(RiskManagerActivity.this, RiskActivityActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_function_2:
                intent.setClass(RiskManagerActivity.this, RiskOperationActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_function_3:
                intent.setClass(RiskManagerActivity.this, RiskProductionActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_function_4:
                intent.setClass(RiskManagerActivity.this, RiskCheckActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_function_5:
                intent.setClass(RiskManagerActivity.this, RiskDeviceActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_function_6:
                intent.setClass(RiskManagerActivity.this, RiskTechnologyActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_function_7:
                intent.setClass(RiskManagerActivity.this, RiskEnvironmentActivity.class);
                startActivity(intent);
                break;
        }
    }

    /**
     * 风险类别
     */
    private void initRiskClass() {
        DropDownInput dropDownInput = new DropDownInput();
        List<String> categoryList = new ArrayList<>();
        categoryList.add("9020");
        List<String> functionList = new ArrayList<>();
        dropDownInput.setCategoryCodeArray(categoryList);
        dropDownInput.setFunctionCodeArray(functionList);
        dropDownInput.setIsGetFactoryList(false);

        Call<String> call = HttpManager.getInstance().requestService().DROP_DOWN(HttpManager.getInstance().requestBody(dropDownInput));
        HttpManager.getInstance().requestCallBack(call, new CommonCallBack<String>() {
            @Override
            public void requestSuccess(String result) {
                DropDownOutput dropDownOutput = new Gson().fromJson(result, DropDownOutput.class);
                if (dropDownOutput.getResult().equals("Success")) {
                    List<DropDownOutput.DataBean.DataDicListBean> dataDicList = dropDownOutput.getData().getDataDicList();
                    for (DropDownOutput.DataBean.DataDicListBean dataDicListBean : dataDicList) {
                        if (dataDicListBean.getKey().equals("9020")) {
                            List<DropDownOutput.DataBean.DataDicListBean.ValueBean> valueBeanList = dataDicListBean.getValue();
                            DropDownOutput.DataBean.DataDicListBean.ValueBean valueBean = new DropDownOutput.DataBean.DataDicListBean.ValueBean();
                            valueBean.setKey("");
                            valueBean.setValue("全部");
                            MyApplication.getInstance().mRiskClassList.clear();
                            MyApplication.getInstance().mRiskClassList.add(valueBean);
                            MyApplication.getInstance().mRiskClassList.addAll(valueBeanList);
                        }
                    }
                } else {
                    ToastUtils.getInstance().showShortToastBottom(dropDownOutput.getMessage());
                }
            }

            @Override
            public void requestError(String errorMsg) {
                ToastUtils.getInstance().showShortToastBottom(errorMsg);
            }
        });
    }

    /**
     * 风险级别
     */
    private void initRiskLevel() {
        DropDownInput dropDownInput = new DropDownInput();
        List<String> categoryList = new ArrayList<>();
        categoryList.add("9010");
        List<String> functionList = new ArrayList<>();
        dropDownInput.setCategoryCodeArray(categoryList);
        dropDownInput.setFunctionCodeArray(functionList);
        dropDownInput.setIsGetFactoryList(false);

        Call<String> call = HttpManager.getInstance().requestService().DROP_DOWN(HttpManager.getInstance().requestBody(dropDownInput));
        HttpManager.getInstance().requestCallBack(call, new CommonCallBack<String>() {
            @Override
            public void requestSuccess(String result) {
                DropDownOutput dropDownOutput = new Gson().fromJson(result, DropDownOutput.class);
                if (dropDownOutput.getResult().equals("Success")) {
                    List<DropDownOutput.DataBean.DataDicListBean> dataDicList = dropDownOutput.getData().getDataDicList();
                    for (DropDownOutput.DataBean.DataDicListBean dataDicListBean : dataDicList) {
                        if (dataDicListBean.getKey().equals("9010")) {
                            List<DropDownOutput.DataBean.DataDicListBean.ValueBean> valueBeanList = dataDicListBean.getValue();
                            DropDownOutput.DataBean.DataDicListBean.ValueBean valueBean = new DropDownOutput.DataBean.DataDicListBean.ValueBean();
                            valueBean.setKey("");
                            valueBean.setValue("全部");
                            MyApplication.getInstance().mRiskLevelList.clear();
                            MyApplication.getInstance().mRiskLevelList.add(valueBean);
                            MyApplication.getInstance().mRiskLevelList.addAll(valueBeanList);
                        }
                    }
                } else {
                    ToastUtils.getInstance().showShortToastBottom(dropDownOutput.getMessage());
                }
            }

            @Override
            public void requestError(String errorMsg) {
                ToastUtils.getInstance().showShortToastBottom(errorMsg);
            }
        });
    }
}
